//
//  BYJFinishSearchViewController.m
//  BLDMusics
//
//  Created by 白云杰 on 16/3/24.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "BYJFinishSearchViewController.h"

@interface BYJFinishSearchViewController ()<UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate>

@property (nonatomic, retain) UITableView *finishSearchTableView;
@property (nonatomic, retain) NSArray *dataSource;

@end

@implementation BYJFinishSearchViewController
{
    UIPercentDrivenInteractiveTransition *percentTransition;
}

- (void)viewWillAppear:(BOOL)animated {
    //self.navigationController.delegate = self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self dataHandle];
    [self createTableView];
    
    UIScreenEdgePanGestureRecognizer *edgeGes = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgePan:)];
    edgeGes.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:edgeGes];
    
}

- (void)createTableView {
    
    self.finishSearchTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH - 49 - 64) style:UITableViewStylePlain];
    _finishSearchTableView.backgroundColor = [UIColor whiteColor];
    _finishSearchTableView.delegate = self;
    _finishSearchTableView.dataSource = self;
    [self.view addSubview:_finishSearchTableView];
    [_finishSearchTableView registerClass:[BYJCarouselTableViewCell class] forCellReuseIdentifier:@"BYJCarouselTableViewCell"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BYJCarouselTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BYJCarouselTableViewCell" forIndexPath:indexPath];
    BYJsongListModel *model = _dataSource[indexPath.row];
    cell.model = model;
    cell.indexPath = indexPath.row + 1;
    cell.block = ^(BYJmvListModel *model) {
        
        BYJMVViewController *mvVc = [[BYJMVViewController alloc] init];
        mvVc.model = model;
        mvVc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:mvVc animated:YES];
    };

    
    return cell;
}

- (void)dataHandle {
    
    NSString *url = [NSString stringWithFormat:@"http://search.dongting.com/song/search?q=%@&page=1&size=50&app=ttpod&v=v8.1.5.2016022418&user_id=407639422&mid=iPhone7%%2C2&f=f320&s=s310&imsi=&hid=&splus=9.2.1&active=1&net=2&openudid=921fb117b843753583dbf257222bb96a79d3c64e&idfa=5B1500CB-A041-4C95-951A-F6408B90A0BF&utdid=Vule3TyuSLwDAKd%%2FfPxZrXTy&alf=201200&bundle_id=com.ttpod.music&latitude=38.88264026030794&longtitude=121.5392442886918&access_token=aaaa74e14fbd80b9ff753dce5dfb32d5", _str];
    
    [APPTools GETWithURL:url par:nil success:^(id responseObject) {
    
        self.dataSource = [BYJsongListModel modelHandleWithArray:responseObject[@"data"]];
        [_finishSearchTableView reloadData];

    } filed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController {
    return percentTransition;
}


-(void)edgePan:(UIPanGestureRecognizer *)recognizer{
    CGFloat per = [recognizer translationInView:self.view].x / (self.view.bounds.size.width);
    per = MIN(1.0,(MAX(0.0, per)));
    
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        percentTransition = [[UIPercentDrivenInteractiveTransition alloc]init];
        [self.navigationController popViewControllerAnimated:YES];
    }else if (recognizer.state == UIGestureRecognizerStateChanged){
        [percentTransition updateInteractiveTransition:per];
    }else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled){
        if (per > 0.3) {
            [percentTransition finishInteractiveTransition];
        }else{
            [percentTransition cancelInteractiveTransition];
        }
        percentTransition = nil;
    }
}


- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC{
    if (operation == UINavigationControllerOperationPop) {
        PingInvertTransition *pingInvert = [PingInvertTransition new];
        return pingInvert;
    }else{
        return nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
